import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

const store = new Vuex.Store({
  state: {
    loading: false,
    role: "", // 目前手动切换该值来切换token
    navBarInputValue: "",
    loadingCount: 0, // 增加axios拦截统计修复连续loading问题
    token: "",
    applyFor: 0, //专家入驻审核是否通过的标识
    AppToken: "",
  },
  getters: {
    getLoading(state) {
      return state.loading;
    },
    inputValue(state) {
      return state.navBarInputValue;
    },

    isExpert(state) {
      return state.role;
      // 0 是蔗农  1是专家
    },

    getToken(state) {
      return state.token || localStorage.getItem("token");
    },

    getAppToken(state) {
      return state.AppToken;
    },
  },
  mutations: {
    setLoading(state, loading) {
      if (loading) {
        state.loadingCount++;
        state.loading = loading;
      } else {
        state.loadingCount--;
        if (state.loadingCount === 0) state.loading = loading;
      }
    },
    setInputValue(state, value) {
      state.navBarInputValue = value;
    },
    setToken(state, token) {
      state.token = token;
      localStorage.setItem("token", token);
    },
    delToken(state) {
      state.token = "";
      localStorage.removeItem("token");
    },
    setRole(state, role) {
      state.role = role;
    },
    changeApplyFor(state, payload) {
      state.applyFor = payload;
      console.log(state.applyFor);
    },
    setAppToken(state, payload) {
      state.AppToken = payload;
    },
  },
});

export default store;
